#!/bin/bash

currentdir=$(cd "$(dirname "$0")"; pwd)
echo "build directory: ${currentdir}."

echo -e "\nbuild pcre..."
if [ ! -d "$currentdir/build/linux" ]; then 
	mkdir -p "$currentdir/build/linux"
else
	rm -rf ${currentdir}/build/linux/*
fi

export TOOLCHAIN="/opt/musl-gcc/gcc-harmony-bin/aarch64/bin"
export TARGET=aarch64-linux-musleabi

export AR=$TOOLCHAIN/$TARGET-ar
export CC=$TOOLCHAIN/$TARGET-gcc
export AS=$TOOLCHAIN/$TARGET-as
export CXX=$TOOLCHAIN/$TARGET-g++
export LD=$TOOLCHAIN/$TARGET-ld
export RANLIB=$TOOLCHAIN/$TARGET-ranlib
export STRIP=$TOOLCHAIN/$TARGET-strip
export OBJCOPY=$TOOLCHAIN/$TARGET-objcopy
#./configure --host $TARGET
#make


autoreconf
./configure CFLAGS=-fPIC  --enable-utf8 --enable-shared=no  --prefix=/usr/local --host $TARGET 
make
make install DESTDIR=${currentdir}/build/linux

if [ ! -f "${currentdir}/build/linux/usr/local/lib/libpcre.a" ]; then exit 1; fi
rm -rf ${currentdir}/../../../lib/libpcr*.a
cp -rv "${currentdir}/build/linux/usr/local/lib/libpcre.a" ${currentdir}/../../../lib
cp -rv "${currentdir}/build/linux/usr/local/lib/libpcrecpp.a" ${currentdir}/../../../lib
cp -rv "${currentdir}/build/linux/usr/local/lib/libpcreposix.a" ${currentdir}/../../../lib

make clean
rm -rf ${currentdir}/build/linux/*

echo "====== pcre build success! ======"
exit 0
